package top.mcwebsite.playandroid.business.qa.ui

import android.annotation.SuppressLint
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.LineBreak
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
import top.mcwebsite.playandroid.business.articles.api.model.ArticleModel
import top.mcwebsite.playandroid.business.qa.vm.QAIntent
import top.mcwebsite.playandroid.business.qa.vm.QAUiEffect
import top.mcwebsite.playandroid.business.qa.vm.QAViewModel
import java.text.SimpleDateFormat
import java.util.*
import top.mcwebsite.lib.route.api.EasyRouter
import top.mcwebsite.lib.router.annotation.Route
import top.mcwebsite.playandroid.common.constant.RouteConfig
import top.mcwebsite.playandroid.common.list.EasyLazyListPageColumn

@Route(RouteConfig.QA.PATH)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun QAPage() {
    val viewModel: QAViewModel = viewModel()
    viewModel.HandleUiEffect {
        when (it) {
            is QAUiEffect.GoQADetailPageUiEffect -> {
                EasyRouter.navigate("${RouteConfig.QADetail}/${it.qaItem.id}") {
                    withParcelable(RouteConfig.QADetail.Param.ARTICLE, it.qaItem)
                }
            }
        }
    }
    Scaffold(
        topBar = {
            TopAppBar(title = {
                Text(text = "问答")
            })
        }
    ) {
        QAPageContent(
            viewModel,
            modifier = Modifier.padding(it)
        ) { intent ->
            viewModel.sendIntent(intent)
        }
    }
}

@Composable
fun QAPageContent(
    viewModel: QAViewModel,
    modifier: Modifier,
    sendIntent: (QAIntent) -> Unit
) {
    val lazyItems = viewModel.pagerFlow.collectAsLazyPagingItems()
    EasyLazyListPageColumn(
        modifier = modifier,
        lazyPagingItems = lazyItems, loadingMoreError = {}) { index, item ->
        QAItemContent(item) {
            sendIntent(QAIntent.OnClickQAItem(it))
        }
        if (index < lazyItems.itemCount - 1) {
            Divider(modifier = Modifier.padding(horizontal = 16.dp, vertical = 10.dp))
        }
    }
}

@Composable

fun QAItemContent(model: ArticleModel, onClick: (item: ArticleModel) -> Unit) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp)
            .padding(top = 16.dp)
            .clickable {
                onClick(model)
            }
    ) {
        Text(text = model.title, style = MaterialTheme.typography.titleMedium.copy(lineBreak = LineBreak.Simple))
        Row(
            modifier = Modifier.padding(vertical = 5.dp)
        ) {
            Text(text = model.superChapterName, style = MaterialTheme.typography.labelMedium)
            Spacer(modifier = Modifier.width(5.dp))
            Text(text = model.chapterName, style = MaterialTheme.typography.labelMedium)
        }
        Row(modifier = Modifier.padding(vertical = 5.dp)) {
            Text(text = "作者: ${model.author}", style = MaterialTheme.typography.labelMedium)
            Spacer(modifier = Modifier.weight(1f))
            Text(text = model.niceDate, style = MaterialTheme.typography.labelMedium)
        }
    }
}

@SuppressLint("SimpleDateFormat")
val dateFormat = SimpleDateFormat("yyyy-mm-dd")

fun getDate(time: Long) =
    dateFormat.format(Date(time))
